package com.xinyue.ext.reader.dao;

import org.dom4j.Element;

import com.xinyue.ext.Label;
import com.xinyue.ext.reader.ParamHolder;
import com.xinyue.ext.util.ListWrapper;

/**
 * 读取XML文件中的entity标签中的dao信息并返回封装对象
 * 
 * 验证：此对象无需再对entity属性进行验证，在BeanReader中已经验证
 */
public class DaoReader implements Label{
	//参数集
	private static ParamHolder holder;
	
	//私有构造器
	private DaoReader() {
	}
	//内部类
	private static class SingletonHolder{
        private static DaoReader instance = new DaoReader();  
    }
	/**
	 * 获得对象实例
	 */
	public static DaoReader getInstance(ParamHolder holder) {
		DaoReader.holder = holder;
		//dao对象的import文件路径集合
		DaoReader.holder.setDaoImpt(new ListWrapper());
		DaoReader.holder.setDaoImptExt(new ListWrapper());
		return SingletonHolder.instance;
	}
	
	/**
	 * 读取finder信息并封装至Entity对象
	 * 此方法传入entity标签下的所有finder标签，通过遍历匹配封装至daoData对象
	 */
	public void read(Element entity) throws Exception {
		
		//读取many和one标签数据
		RelyProcess.getInstance(holder).read(entity);
		
		//finder标签
		Element finder = entity.element(finder_);
		if(finder != null) {
			//读取query
			QueryProcess.getInstance(holder).read(finder);
			//读取hql
			HqlProcess.getInstance(holder).read(finder);
			//读取hql
			SqlProcess.getInstance(holder).read(finder);
			
			//set import
			holder.getEntity().getDao().setImports(holder.getDaoImpt().getList());
			holder.getEntity().getDao().setImportExt(holder.getDaoImptExt().getList());
		}
	}
}